package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Actor;
import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.ActorRepository;
import com.example.moviecms2.repository.MovieRepository;
import com.example.moviecms2.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author 杨友奇
 * @Time 2018/4/18
 * 负责电影详情页面的添加电影角色，查询演员个人信息，编辑，删除
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private ActorRepository actorRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private MovieRepository movieRepository;


    //跳转到演员添加界面
    @RequestMapping(value = "/add/{movie_id}", method = RequestMethod.GET)
    public String toAdd(Model model, @PathVariable Long movie_id) {
        Movie movie = movieRepository.findOne(movie_id);
        model.addAttribute("info", movie);
        return "role/add";
    }

    //重定向
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam String role_name,
                        @RequestParam  String actor_name,
                        @RequestParam  Long movie_id){


        //通过电影ID获取电影信息
        Movie movie = movieRepository.findOne(movie_id);

        Actor actor = new Actor();
        actor.setName(actor_name);
        movie.addRole(actor, role_name);
        movieRepository.save(movie);

        return "redirect:/movie/detail/" + movie_id;
    }

    //查询演员个人信息
    @RequestMapping(value = "/select/{id}", method = RequestMethod.GET)
    public String toSelect(Model model, @PathVariable Long id) {
        Actor actor = actorRepository.findOne(id);
        model.addAttribute("info", actor);
        return "role/select";
    }


    //演员编辑页面
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id) {
        Role role = roleRepository.findOne(id);
        model.addAttribute("info", role);
        return "role/edit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Long id,
                         @RequestParam String actor_name,
                         @RequestParam String role_name) {
        //获取ID
        Role role = roleRepository.findOne(id);
        Actor actor = actorRepository.findOne(id);
        //设置数值
        role.setName(role_name);
        actor.setName(actor_name);
        //更新变量
        actorRepository.save(actor);
        roleRepository.save(role);

        return "redirect:/movie/index";
    }


    //删除演员
    //涉及到联级 假如删除的是演员则电影跟角色全部删除
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String Delete(@PathVariable Long id) {
        actorRepository.delete(id);
        return "redirect:/movie/index";
    }

}
